//
//  HJInformationLayout.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJInformationLayout.h"
#import "HJFollowModel.h"
#import "HJAttributedStringFactory.h"

@interface HJInformationLayout()

@property (nonatomic, strong, readwrite) HJInformationModel *model;

@end


@implementation HJInformationLayout

MJLogAllIvars

- (instancetype)init {
    if (self = [super init]) {
        self.isFold = YES;
    }
    return self;
}

+ (HJInformationLayout *)layoutWithModel:(HJInformationModel *)model {
    HJInformationLayout *informationLayout = [[HJInformationLayout alloc] init];
    informationLayout.model = model;
    informationLayout.isFold = YES;
    [informationLayout calculateLayout];
    return informationLayout;
}

- (void)calculateLayout {
    
    _authorInfoRect = CGRectMake(0, 15, KScreenWidth, 40);
    NSString *title = nil;
    UIFont *titleFont = nil;
    if (isEmptyString(self.model.postTitle)) {
        title = self.model.content;
        titleFont = UIFontMake(14);
    } else {
        title = self.model.postTitle;
        titleFont = UIFontMediumMake(14);
    }
    
    if (_model.images.count > 0) {
        _mediaDatas = _model.images;
        self -> _isVideo = NO;
    } else {
        _mediaDatas = _model.videos;
        self -> _isVideo = YES;
    }
 
    HJAttributedStringFactory *attributedStringFactory = [[HJAttributedStringFactory alloc] initWithString:title maxWidth:KScreenWidth - leftRightMargin * 2 font:titleFont lineSpacing:3 alignment:NSTextAlignmentLeft];
    self.attributedString = attributedStringFactory.attributedString;
    CGSize textSize = attributedStringFactory.size;
        
    if (isEmptyString(self.model.postTitle)) {
        if (attributedStringFactory.numberOfLines > 3) {
            textSize.height = [attributedStringFactory numberLineHeight:3];
            _isShowAllButton = YES;
            if (_isFold) {
                textSize.height = [attributedStringFactory numberLineHeight:3];
            } else {
                textSize.height = attributedStringFactory.size.height;
            }
        }
    }
    
    self.model.publishTime = [[NSDate dateWithTimeIntervalSince1970:self.model.publishTimestamp] stringWithFormat:@"dd MMM, yyyy HH:mm"];
    
    CGSize allButtonSize = [(_isFold ? @"All" : @"Conceal") getStringSizeWithWidth:100 font:UIFontMake(12)];

    _contentRect = CGRectMake(leftRightMargin, CGRectGetMaxY(_authorInfoRect) + 15, textSize.width, textSize.height);
    if (_isShowAllButton) {
        _foldButtonRect =  CGRectMake(_contentRect.origin.x, CGRectGetMaxY(_contentRect) + 4, allButtonSize.width + 20, allButtonSize.height);
    } else {
        _foldButtonRect =  CGRectMake(_contentRect.origin.x, CGRectGetMaxY(_contentRect) + 4, 0, 0);
    }
    
    CGFloat imageRectY = CGRectGetMaxY(_foldButtonRect) + 10;
    if (isEmptyString(self.model.postTitle) && isEmptyString(self.model.content)) {
        imageRectY = CGRectGetMaxY(_authorInfoRect) + 15;
    }
    
    if (_mediaDatas.count > 0){
        if (_mediaDatas.count < 3) {
            _imagesRect = CGRectMake(_contentRect.origin.x, imageRectY, KScreenWidth - _contentRect.origin.x * 2, ZOOM(164));
        } else {
            _imagesRect = CGRectMake(_contentRect.origin.x, imageRectY, KScreenWidth - _contentRect.origin.x * 2, ZOOM(106));
        }
    } else {
        _imagesRect = CGRectMake(_contentRect.origin.x, imageRectY, KScreenWidth, 0);
    }

    _operateRect = CGRectMake(_contentRect.origin.x, CGRectGetMaxY(_imagesRect) + 12, KScreenWidth - _contentRect.origin.x * 2, 32);
    _height = CGRectGetMaxY(_operateRect) + 12;
}

+ (HJInformationLayout *)followLayoutWithDatas:(NSArray *)datas {
    HJInformationLayout *informationLayout = [[HJInformationLayout alloc] init];
    informationLayout.isFollow = YES;
    informationLayout.isFold = NO;
    informationLayout.height = 50 + datas.count * 60;
    informationLayout.followDatas = datas;
    return informationLayout;
}



@end
